﻿using System;
using System.Text;
using System.Collections.Generic;

namespace TidyBlog.Data
{
    /// <summary>
    /// SQL命令生成类
    /// </summary>
    public abstract class Command
    {
        /// <summary>
        /// 表名
        /// </summary>
        protected readonly string[] Tables;
        /// <summary>
        /// WHERE子句条件
        /// </summary>
        public List<WhereCondition> WhereConditions
        {
            get;
            protected set;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="table">表格名</param>
        public Command(string table)
        {
            Tables = new[] { table };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tables">表格名</param>
        public Command(string[] tables)
        {
            Tables = (string[])tables.Clone();
        }

        /// <summary>
        /// 获取表格字符串
        /// </summary>
        /// <returns>表格字符串</returns>
        protected string GetTableString()
        {
            var quote = DbHelper.Provider.QuoteSymbol;

            if (Tables.Length == 1)
            {
                return String.Format(quote, Tables);
            }
            else
            {
                var result = new StringBuilder();

                foreach (var table in Tables)
                {
                    if (result.Length > 0)
                    {
                        result.Append(",");
                    }

                    result.Append(String.Format(quote, table));
                }

                return result.ToString();
            }
        }

        /// <summary>
        /// 指定查询条件
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>当前命令</returns>
        public Command Where(WhereCondition condition)
        {
            WhereConditions = new List<WhereCondition>();
            WhereConditions.Add(condition);

            return this;
        }
        /// <summary>
        /// 指定查询条件
        /// </summary>
        /// <param name="conditions">条件</param>
        /// <returns>当前命令</returns>
        public Command Where(IEnumerable<WhereCondition> conditions)
        {
            WhereConditions = new List<WhereCondition>(conditions);

            return this;
        }

        /// <summary>
        /// 获取WHERE子句字符串
        /// </summary>
        /// <returns>WHERE子句字符串</returns>
        protected string GetWhereString()
        {
            if (WhereConditions == null || WhereConditions.Count == 0)
            {
                return String.Empty;
            }

            var result = new StringBuilder();
            LogicalOperator lastConnOper = LogicalOperator.AND;
            var paramPrefix = DbHelper.Provider.ParamPrefix;
            var quote = DbHelper.Provider.QuoteSymbol;

            foreach (var condition in WhereConditions)
            {
                if (result.Length > 0)
                {
                    if (condition.ConnectionOperator != lastConnOper)
                    {
                        result.Insert(0, "(");
                        result.Append(")");
                    }
                    result.Append(" ");
                    result.Append(condition.ConnectionOperator.ToString());
                    result.Append(" ");
                }

                result.Append(String.Format(quote, condition.Column));
                result.Append(DbHelper.Provider.GetWhereOperatorValue(condition.Operator));
                if (condition.IsParamValue)
                {
                    result.Append(paramPrefix);
                }
                result.Append(condition.Value);

                lastConnOper = condition.ConnectionOperator;
            }

            result.Insert(0, " WHERE ");

            return result.ToString();
        }
    }
}
